<template>
	<div class="wrapper">
		<header>
			商家信息
		</header>
		<!-- logo -->
		<div class="business-logo">
			<img v-bind:src="business.businessImg">
		</div>
		<!-- 商家信息 -->
		<div class="business-info">
			<h1>{{business.businessName}}</h1>
			<p>&#165;{{business.starPrice}}起送 &#165;{{business.deliveryPrice}}配送</p>
			<p>{{business.businessExplain}}</p>
		</div>
		<!-- 食品列表 -->
		<ul class="food">
			<li v-for="(item,index) in foodArr">
				<div class="food-left">
					<img :src="item.foodImg">
					<div class="food-left-info">
						<h3>{{item.foodName}}</h3>
						<p>{{item.foodExplain}}</p>
						<p>&#165;{{item.foodPrice}}</p>
					</div>
				</div>
				<div class="food-right">
					<div>
						<i class="fa fa-minus-circle" v-show="item.quantity!=0" @click="minus(index)"></i>
					</div>
					<p><span v-show="item.quantity!=0">{{item.quantity}}</span></p>
					<div>
						<i class="fa fa-plus-circle" @click="add(index)"></i>
					</div>
				</div>
			</li>
		</ul>

		<div class="cart">
			<div class="cart-left">
				<div class="cart-left-icon">
					<i class="fa fa-shopping-cart"></i>
					<div class="cart-left-icon-quantity" v-show="totalQuantity!=0">{{totalQuantity}}</div>
				</div>
				<div class="cart-left-info">
					<p>&#165;{{totalPrice}}</p>
					<p>另需要配送费{{business.deliveryPrice}}元</p>
				</div>
			</div>
			<div class="cart-right">
				<!-- 不够起送费网页 -->
				<div class="cart-right-item" v-show="totalSettle<business.starPrice" style="background-color: #535353;cursor: default;">
					&#165;{{business.starPrice}}起送
				</div>

				<!-- 够起送费的网页 -->
				<div class="cart-right-item" v-show="totalSettle>=business.starPrice" @click="toOrder()">
					去结算
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default{
		name:'BusinessInfo',
		data(){
			return{
				businessId:this.$route.query.businessId,
				business:{},
				foodArr:[],
				user:{},
				cartArr:[]
			}
		},
		created() {
			//获取当前用户信息
			this.user = this.$getSessionStorage('user');
			
			//根据商家id查询商家信息
			this.$axios.post('BusinessController/getBusinessById',
			this.$qs.stringify({businessId:this.businessId}))
			.then(resp =>{
				this.business =resp.data;
				
			})
			.catch(err=>{
				console.log(err)
			})
			
			
			//再根据商家ID查询所属食品信息
			this.$axios.post('FoodController/listFoodByBusinessId',
			this.$qs.stringify({businessId:this.businessId}))
			.then(resp =>{
				this.foodArr =resp.data;
				console.log(this.foodArr)
				
				//如果用户已经登陆, 那么我们查询下它在购物车中选购了该商家的哪些食品
				if(this.user != null){
					this.listCart();
				}
			})
			.catch(err=>{
				console.log(err)
			})
		},
		methods:{
			listCart(){
				this.$axios.post('CartController/listCart',
				this.$qs.stringify({userId:this.user.userId,businessId:this.businessId}))
				.then(response=>{
					var cartArr = response.data;
					//遍历食品列表
					for(var foodItem of this.foodArr){
						foodItem.quantity = 0 ;//创建一个属性,存放每种食品购物车中的数量
						for(var  cartItem of cartArr){
							if(cartItem.foodId == foodItem.foodId){
								foodItem.quantity += cartItem.quantity;
							}
						}
					}
					this.foodArr.sort();
				})
				.catch(err=>{
					
				})				
			},
			add(index){
				//首先做登陆验证
				if(this.user == null){
					this.$router.push({path:'/login'});
					return;
				}
				
				if(this.foodArr[index].quantity ==0){
					//做insert操作
					this.saveCart(index);
				}else{
					//做update操作
					this.updateCart(index,1);
				}
			},
			minus(index){
				//首先做登陆验证
				if(this.user == null){
					this.$router.push({path:'/login'});
					return;
				}
				
				if(this.foodArr[index].quantity >1){
					//做insert操作
					this.updateCart(index,-1);
				}else{
					//做update操作
					this.removeCart(index);
				}
			},
			saveCart(index){
				this.$axios.post('CartController/saveCart',
				this.$qs.stringify({
					businessId:this.businessId,
					userId:this.user.userId,
					foodId:this.foodArr[index].foodId
				}))
				.then(res=>{
					if(res.data==1){
						this.foodArr[index].quantity =1;
						this.foodArr.sort();
					}else{
						alert('向购物车添加商品失败!')
					}
				})
				.catch(err=>{
					console.log(err)
				})
			},
			updateCart(index,num){
				this.$axios.post('CartController/updateCart',
				this.$qs.stringify({
					businessId:this.businessId,
					userId:this.user.userId,
					foodId:this.foodArr[index].foodId,
					quantity:this.foodArr[index].quantity+num
				}))
				.then(res=>{
					if(res.data==1){
						this.foodArr[index].quantity =this.foodArr[index].quantity+num;
						this.foodArr.sort();
					}else{
						alert('向购物车添加商品失败!')
					}
				})
				.catch(err=>{
					console.log(err)
				})
			},
			removeCart(index){
				this.$axios.post('CartController/removeCart',
				this.$qs.stringify({
					businessId:this.businessId,
					userId:this.user.userId,
					foodId:this.foodArr[index].foodId
				}))
				.then(res=>{
					if(res.data==1){
						this.foodArr[index].quantity =0;
						this.foodArr.sort();
					}else{
						alert('向购物车添加商品失败!')
					}
				})
				.catch(err=>{
					console.log(err)
				})
			},
			toOrder(){
				this.$router.push({path:'/orders',query:{businessId:this.businessId}})
			}
		},
		computed:{
			//食品总价格  this.totalPrice 
			totalPrice(){
				var total = 0;
				for(var item of this.foodArr){
					total +=item.foodPrice*item.quantity;
				}
				return total;
			},
			//食品总数量
			totalQuantity(){
				var quantity =0 ;
				for(var item of this.foodArr){
					quantity +=item.quantity;
				}
				return quantity;
			},
			//食品+配送费合计
			totalSettle(){
				return this.totalPrice+this.business.deliveryPrice;
			}
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		display: flex;
		justify-content: center;
		align-items: center;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;
	}

	/****************** 商家信息图片部分 ******************/
	.wrapper .business-logo {
		width: 100%;
		height: 35vw;
		margin-top: 12vw;
		/*使用上外边距避开header部分*/

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .business-logo img {
		border-radius: 5px;
	}

	/* 商家信息 */
	.wrapper .business-info {
		width: 100%;
		height: 20vw;

		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.wrapper .business-info h1 {
		font-size: 4vw;
	}

	.wrapper .business-info p {
		font-size: 3vw;
		color: #666;
		margin-top: 1vw;
	}

	/* 食品列表 */
	.wrapper .food {

		padding-bottom: 14vw;
		/*预留底部菜单的高度*/

	}

	.wrapper .food li {
		width: 100%;
		box-sizing: border-box;
		padding: 2vw;
		border-bottom: 1px solid #DDD;

		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .food li .food-left {
		display: flex;
		align-items: center;
	}

	.wrapper .food li .food-left img {
		width: 20vw;
		height: 20vw;
		margin-right: 3vw;
	}

	.wrapper .food li .food-left .food-left-info h3 {
		font-size: 3.5vw;
		color: #555;
	}

	.wrapper .food li .food-left .food-left-info p {
		font-size: 3vw;
		color: #888;
		margin-top: 2vw;
	}

	.wrapper .food li .food-right {
		width: 16vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .food li .food-right .fa-minus-circle {
		font-size: 5.5vw;
		color: #999;
		cursor: pointer;
	}

	.wrapper .food li .food-right p {
		font-size: 3.5vw;
		color: #333;
		margin: 0 1vw;
	}

	.wrapper .food li .food-right .fa-plus-circle {
		font-size: 5.5vw;
		color: #0097FF;
		cursor: pointer;
	}

	/* 购物车 */
	.wrapper .cart {
		width: 100%;
		height: 14vw;

		position: fixed;
		left: 0;
		bottom: 0;

		display: flex;
	}

	.wrapper .cart .cart-left {
		flex: 2;
		background-color: #505050;
		display: flex;

	}

	.wrapper .cart .cart-left .cart-left-icon {
		width: 16vw;
		height: 16vw;
		box-sizing: border-box;
		border: 1.6vw solid #444;
		border-radius: 8vw;
		background-color: #0097FF;
		font-size: 7vw;
		color: #fff;

		display: flex;
		justify-content: center;
		align-items: center;

		position: relative;
		top: -4vw;
		left: 3vw;
	}

	.wrapper .cart .cart-left .cart-left-icon .cart-left-icon-quantity {
		width: 5vw;
		height: 5vw;
		background-color: red;
		border-radius: 2.5vw;
		font-size: 3.5vw;
		color: #fff;

		display: flex;
		justify-content: center;
		align-items: center;

		position: absolute;
		right: -1.5vw;
		top: -1.5vw;

	}

	.wrapper .cart .cart-left .cart-left-info {
		margin-left: 4vw;

	}

	.wrapper .cart .cart-left .cart-left-info p:first-child {
		color: #fff;
		font-size: 4.5vw;
		margin-top: 1vw;

	}

	.wrapper .cart .cart-left .cart-left-info p:last-child {
		color: #aaa;
		font-size: 2.8vw;

	}

	.wrapper .cart .cart-right {
		flex: 1;

	}

	/* 够起送费 , 背景色为绿色 */
	.wrapper .cart .cart-right .cart-right-item {
		width: 100%;
		height: 100%;
		color: #fff;
		font-size: 4.5vw;
		font-weight: 700;
		background-color: #38CA73;


		display: flex;
		justify-content: center;
		align-items: center;
	}

	/* 不够起送费 ,背景色为灰色 
.wrapper .cart .cart-right .cart-right-item{
	width: 100%;
	height: 100%;
	color: #fff;
	font-size: 4.5vw;
	font-weight: 700;
	background-color: #535353;
	
	
	display: flex;
	justify-content: center;
	align-items: center;
	
}*/
</style>
